﻿//
// This file is part of AVSReflector project
// Copyright (C) 2011 Yuan Pengfei
//

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using AVSReflector.Core;

namespace AVSReflector
{
    public partial class MainForm : Form, IReflector
    {
        BitStream stream;
        List<Sequence> seqs;

        bool errorflag;
        string errormsg;
        Thread t;

        public MainForm()
        {
            InitializeComponent();
        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            System.Diagnostics.Process.Start(((LinkLabel)sender).Text);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (opendiag.ShowDialog() != DialogResult.OK)
                return;
            button1.Enabled = false;
            runner.Value = runner.Minimum;
            progress.Value = 0;
            try
            {
                byte[] content = System.IO.File.ReadAllBytes(opendiag.FileName);
                progress.Value = 20;
                stream = new BitStream(content);
                seqs = new List<Sequence>();
                errorflag = false;
                errormsg = null;
                while (!stream.EndOfStream())
                {
                    t = new Thread(DoParse);
                    t.Start();
                    while (t.IsAlive)
                    {
                        if (runner.Value == runner.Maximum)
                            runner.Value = runner.Minimum;
                        else
                            runner.Value += 2;
                        Application.DoEvents();
                        Thread.Sleep(100);
                    }
                    t = null;
                    if (errorflag)
                        throw new Exception(errormsg);
                    progress.Value = 20 + (int)(80 * stream.Percentage());
                }
                runner.Value = runner.Maximum;
                progress.Value = 100;
                Reflector r = new Reflector(this, seqs.ToArray());
                r.Text = r.Text + " - [" + opendiag.FileName + "]";
                r.Show();
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message);
                runner.Value = runner.Minimum;
                progress.Value = 0;
                button1.Enabled = true;
                return;
            }
            button1.Enabled = true;
            this.Visible = false;
        }

        void DoParse()
        {
            try
            {
                Sequence s = Sequence.Parse(stream);
                if (s != null)
                    seqs.Add(s);
            }
            catch (System.Exception ex)
            {
                if (ex.GetType() != typeof(ThreadAbortException))
                {
                    errorflag = true;
                    if (ex.GetType() == typeof(System.IndexOutOfRangeException))
                        errormsg = "Invalid AVS format!";
                    else
                        errormsg = ex.Message;
                }
            }
        }

        public void ReflectorClose()
        {
            this.Visible = true;
        }

        private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            // FIXME: this is not good
            if (t != null)
                System.Environment.Exit(0);
        }
    }
}
